home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 12 - Game Aesthetics / MenuBarRoutines.c
Text File  |  1995-06-21  |  5KB  |  163 lines

  1. //==========================================================================//
  2. //                                                                            //
  3. //                                MenuBarRoutines.c                            //
  4. //                                                                            //
  5. //                        A couple of handy routines for                        //
  6. //                        hiding and showing the menu bar.                    //
  7. //                                                                            //
  8. //==========================================================================//
  9.  
  10.  
  11. short    mBarHigh;    // A global variable used by both these routines.
  12.  
  13. void HideMenuBar (void);
  14. void ShowMenuBar (void);
  15. //--------------------------------------------------------------  HideMenuBar
  16. void HideMenuBar (void)
  17. {
  18.             // This function hides the menu bar by
  19.             // calling LMSetMBarHeight() and then painting
  20.             // over the menu bar with black.
  21.  
  22.     Rect            theRect;
  23.     RgnHandle    worldRgn, menuBarRgn;
  24.     GrafPtr        wasPort, tempPort;
  25.     
  26.     if (LMGetMBarHeight() != 0)    // only hide if not hidden
  27.     {
  28.             // First, we’re going to save a pointer to the old
  29.             // port and create a temporary “scratch port” in 
  30.             // which to define our regions and paint over the
  31.             // menu bar.
  32.  
  33.         GetPort(&wasPort);        // remember old port
  34.         tempPort = (GrafPtr)NewPtrClear(sizeof(GrafPort));
  35.         OpenPort(tempPort);        // create temporary new port
  36.         SetPort((GrafPtr)tempPort);
  37.  
  38.             // We get a copy of the height of the menu bar and
  39.             // save it off in the global mBarHigh (a short).
  40.  
  41.         mBarHigh = LMGetMBarHeight();
  42.  
  43.             // Now we tell the Mac the menu bar is gone.
  44.  
  45.         LMSetMBarHeight(0);        // sets low-mem global to 0
  46.         
  47.             // Now we need to get a region that describes the
  48.             // familiar rounded rectangle of the main monitor.
  49.             // In cases with multiple monitors, the global
  50.             // qd.screenBits will be the one with the menu bar...
  51.             // The one whose region we’re concerned with 
  52.             // imitating.
  53.  
  54.         theRect = (**GetGrayRgn()).rgnBBox;
  55.         UnionRect(&theRect, &qd.screenBits.bounds, &theRect);
  56.         worldRgn = NewRgn();        // init new region
  57.         OpenRgn();                // open region for defining
  58.         FrameRoundRect(&theRect, 16, 16);
  59.         CloseRgn(worldRgn);        // just defined the screen 
  60.         
  61.             // Although the region we’ve just defined represents
  62.             // the entire monitor with the menu bar, we’re going
  63.             // to trim this down to a region that represents
  64.             // just the menu bar -- the top strip of the screen.
  65.  
  66.         theRect = qd.screenBits.bounds;
  67.         theRect.bottom = theRect.top + mBarHigh;
  68.         menuBarRgn = NewRgn();        // define yet another region
  69.         RectRgn(menuBarRgn, &theRect);
  70.         
  71.             // Now we have worldRgn = the whole screen and
  72.             // menuBarRgn = just the top strip where the menu bar
  73.             // is. By finding their intersection, we manufacture
  74.             // an exact shape of the menu bar -- rounded corners
  75.             // only on the top.
  76.  
  77.         SectRgn(worldRgn, menuBarRgn, menuBarRgn);
  78.         DisposeRgn(worldRgn);        // no longer need this region
  79.         
  80.             // Now that we have our menu bar region, we need to 
  81.             // add it to our temporary ports’ visRgn. In this
  82.             // way we can use QuickDraw to paint over it.
  83.  
  84.         UnionRgn(tempPort->visRgn, 
  85.                 menuBarRgn, 
  86.                 tempPort->visRgn);
  87.  
  88.         DisposeRgn(menuBarRgn);    // no longer need this region
  89.         
  90.         PaintRect(&theRect);        // paint over the menu bar
  91.         
  92.         ClosePort(tempPort);        // clean up
  93.         SetPort((GrafPtr)wasPort);
  94.     }
  95. }
  96.  
  97.  
  98. //--------------------------------------------------------------  ShowMenuBar
  99. // Showing the menu bar is, unfortunately, just as complex (only in reverse):
  100.  
  101. void ShowMenuBar (void)
  102. {
  103.     Rect            theRect;
  104.     GrafPtr        wasPort, tempPort;
  105.     RgnHandle    worldRgn, menuBarRgn;
  106.     
  107.     if (LMGetMBarHeight() == 0)    // only do if menu bar hidden
  108.     {
  109.         GetPort(&wasPort);        // do the port thing again
  110.         tempPort = (GrafPtr)NewPtrClear(sizeof(GrafPort));
  111.         OpenPort(tempPort);
  112.         SetPort((GrafPtr)tempPort);
  113.         
  114.             // Restore the menu bar height in low-mem global
  115.  
  116.         LMSetMBarHeight(mBarHigh);
  117.         
  118.             // Create a screen region again (with rounded corners)
  119.  
  120.         theRect = (**GetGrayRgn()).rgnBBox;
  121.         UnionRect(&theRect, &qd.screenBits.bounds, &theRect);
  122.         worldRgn = NewRgn();
  123.         OpenRgn();
  124.         FrameRoundRect(&theRect, 16, 16);
  125.         CloseRgn(worldRgn);
  126.         
  127.             // Create a rectangular menu bar region again
  128.  
  129.         theRect = qd.screenBits.bounds;
  130.         theRect.bottom = theRect.top + mBarHigh;
  131.         menuBarRgn = NewRgn();
  132.         RectRgn(menuBarRgn, &theRect);
  133.         
  134.             // Intersect the 2 regions to arrive at a rounded
  135.             // menu bar region again. 
  136.  
  137.         SectRgn(worldRgn, menuBarRgn, menuBarRgn);    
  138.         DisposeRgn(worldRgn);
  139.  
  140.             // Once again we add it to our tempPort’s visRgn
  141.  
  142.         UnionRgn(tempPort->visRgn, 
  143.                 menuBarRgn, 
  144.                 tempPort->visRgn);
  145.  
  146.             // But this time we “subtract” the menu bar region
  147.             // from the tempPort’s visRgn -- in this way, the
  148.             // menu bar is removed and thus can’t be drawn over.
  149.  
  150.         DiffRgn(tempPort->visRgn, menuBarRgn, tempPort->visRgn);
  151.         DisposeRgn(menuBarRgn);
  152.         
  153.         ClosePort(tempPort);    // clean up
  154.         SetPort((GrafPtr)wasPort);
  155.         
  156.             // At this point, all is well and good, but the Mac
  157.             // hasn’t got around to drawing the menu bar. We need
  158.             // to tell it to do so.
  159.  
  160.         DrawMenuBar();
  161.     }
  162. }
  163.